from django.test import TestCase
from goods.models import Goods, GoodsCategory, GoodsImage
from goods.models import GoodsCategory



# from db_tools.data.category_data import row_data
#
# # 1. 插入商品分类测试数据
# for lev1 in row_data:
#     category1 = GoodsCategory()
#     category1.code = lev1['code']
#     category1.name = lev1['name']
#     category1.category_type = 1
#     category1.save()
#
#     for lev2 in lev1['sub_categorys']:
#         category2 = GoodsCategory()
#         category2.code = lev2['code']
#         category2.name = lev2['name']
#         category2.category_type = 2
#         category2.parent_category = category1
#         category2.save()
#
#         for lev3 in lev2['sub_categorys']:
#             category3 = GoodsCategory()
#             category3.code = lev2['code']
#             category3.name = lev2['name']
#             category3.category_type = 2
#             category3.parent_category = category2
#             category3.save()


from db_tools.data.product_data import row_data

# 2.插入商品测试数据
for goods_info in row_data:

    goods = Goods()
    goods.name = goods_info['name']
    goods.market_price = float(int(goods_info['market_price'].replace('￥', '').replace('元', '')))
    goods.shop_price = float(int(goods_info['sale_price'].replace('￥', '').replace('元', '')))
    goods.goods_brief = goods_info['desc'] if goods_info['desc'] is not None else ''
    goods.goods_desc = goods_info['goods_desc'] if goods_info['goods_desc'] is not None else ''
    goods.goods_front_image = goods_info['images'][0] if goods_info['images'][0] else ''

    # 获取此商品所属分类的名称
    category_name = goods_info['categorys'][-1]
    # 查询此商品所属的分类
    category = GoodsCategory.objects.filter(name=category_name)

    if category:
        goods.category = category[0]
    goods.save()

    for goods_image in goods_info['images']:

        goods_img = GoodsImage()
        goods_img.goods = goods
        goods_img.image = goods_image
        goods_img.save()


